/**
******************************************************************************************
* @file MercureController.c
* @brief Management of the criticality of the temperature.
* @date 29 mars 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "MercureController.h"


/*******************************************************************************************
* PROTOTYPES
********************************************************************************************/

void MercureController_destroyUtilities(MercureController*);

void MercureController_destroy(MercureController**);

void MercureController_defineTemperatureCriticality(MercureController*, float);

int MercureController_registerObserver(MercureController*, Observer*);

int MercureController_unregisterObserver(MercureController*, Observer*);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @return Pointer to a new MercureController
 **/
MercureController* MercureController_create(){
	MercureController* This = (MercureController *) malloc(sizeof(*This));

        //Init functions
        This->destroy = MercureController_destroy;
        This->defineCriticality = MercureController_defineTemperatureCriticality;
	This->subject = subjectNew(This, 4);
	This->registerObserver = MercureController_registerObserver;
	This->unregisterObserver = MercureController_unregisterObserver;

        //Init variable
        This->criticality = NO_TEMPERATURE;

	return This;
}


/**
 * @brief Destroy all of the components of MercureController
 * @param This Pointer to the wanted MercureController
 **/
void MercureController_destroyUtilities(MercureController* This)
{
	This->subject->destroy(This->subject);

}

/**
 * @brief Destructor
 * @param This Double pointer to the wanted MercureController
 **/
void MercureController_destroy(MercureController** This)
{

 	MercureController_destroyUtilities(* This);
	if (*This) {
		free(*This);
		*This = NULL;
	}
}


/**
 * @brief State machine use by the callback function of Mercure Lover for notify observers when the criticality of the temperature changed
 * @param This Pointer to the wanted MercureController
 * @param temperature Value of the temperature given by the bricklet
 **/
void MercureController_defineTemperatureCriticality(MercureController* This, float temperature) {
	switch(This->criticality) {
	case NO_TEMPERATURE:
	        if( temperature > 0 && temperature < T_LC ){
			This->criticality= CRITIC_COLD_TEMPERATURE;

		}
		else if( temperature > T_HC){
			This->criticality= CRITIC_HOT_TEMPERATURE;

		}
		else if( temperature >= T_LC && temperature < T_LM){
			This->criticality= MODERATE_COLD_TEMPERATURE;

		}
                else if( temperature > T_HM && temperature <= T_HC ){
			This->criticality= MODERATE_HOT_TEMPERATURE;

		}
                else{
			This->criticality= NORMAL_TEMPERATURE;

                }
                This->subject->notifyObservers(This->subject);
		break;
	case NORMAL_TEMPERATURE:
		if( temperature>= T_LC && temperature <T_LML  ){
			This->criticality= MODERATE_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature >T_HC ){
			This->criticality= CRITIC_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature < T_LC){
			This->criticality= CRITIC_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else if( temperature <= T_HC  && temperature> T_HMH  ){
			This->criticality= MODERATE_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else{
			This->criticality = NORMAL_TEMPERATURE;

                }
		break;
	case MODERATE_COLD_TEMPERATURE:
		if( temperature <= T_HM && temperature >= T_LMH ){
			This->criticality= NORMAL_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature > T_HC ){
			This->criticality= CRITIC_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature < T_LCL ){
			This->criticality= CRITIC_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else if( temperature <= T_HC  && temperature > T_HM ){
			This->criticality= MODERATE_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else{
			This->criticality = MODERATE_COLD_TEMPERATURE;

                }
		break;
	case MODERATE_HOT_TEMPERATURE:
		if( temperature >= T_LM && temperature <= T_HML ){
			This->criticality= NORMAL_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature > T_HCH ){
			This->criticality= CRITIC_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature < T_LC ){
			This->criticality= CRITIC_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else if( temperature <T_LM && temperature >= T_LC ){
			This->criticality= MODERATE_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else{
			This->criticality = MODERATE_HOT_TEMPERATURE;

                }
		break;
	case CRITIC_COLD_TEMPERATURE:
                if( temperature >= T_LM && temperature <= T_HM ){
			This->criticality= NORMAL_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature > T_HC){
			This->criticality= CRITIC_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature >= T_LC && temperature < T_LM){
			This->criticality= MODERATE_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else if( temperature > T_HM && temperature <= T_HC ){
			This->criticality= MODERATE_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else{
			This->criticality = CRITIC_COLD_TEMPERATURE;

                }
		break;
	case CRITIC_HOT_TEMPERATURE:
		if( temperature >= T_LM && temperature <= T_HM ){
			This->criticality= NORMAL_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature < T_LC){
			This->criticality= CRITIC_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
		else if( temperature >= T_LC && temperature < T_LM){
			This->criticality= MODERATE_COLD_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else if( temperature > T_HM && temperature <= T_HC ){
			This->criticality= MODERATE_HOT_TEMPERATURE;
                        This->subject->notifyObservers(This->subject);

		}
                else{
			This->criticality = CRITIC_HOT_TEMPERATURE;

                }
		break;
	default:
		printf("ERROR : TemperatureController MAE is out!\n  ");
		break;
	}
}


/**
 * @brief Proxy method for Register an observer for the MercureController object.
 * @param This Pointer to the wanted MercureController
 * @param This Pointer to the wanted observer
 * @return The result of the development of the registration (0 = GOOD and 1 = NOT GOOD)
 **/
int MercureController_registerObserver(MercureController* This, Observer* observer) {
	return This->subject->registerObserver(This->subject, observer);
}

/**
 * @brief Proxy method for Unregister observer for the MercureController object.
 * @param This Pointer to the wanted MercureController
 * @param This Pointer to the wanted observer
 * @return The result of the development of the unregistration (0 = GOOD and 1 = NOT GOOD)
 **/
int MercureController_unregisterObserver(MercureController* This, Observer* observer) {
	return This->subject->unregisterObserver(This->subject, observer);
}
